﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;
using System.ComponentModel;


namespace BUS
{
    public class AuthorBUS
    {
        AuthorDAO authorDAO = new AuthorDAO();

        public int InsertAuthor(AuthorDTO author)
        {
            if (authorDAO.CheckExistsCode(author.AuthorCode))
            {
                return 0;
            }

            return authorDAO.InsertAuthor(author);
        }

        public bool CheckExistsCode(string code)
        {
            return authorDAO.CheckExistsCode(code);
        }

        public BindingList<AuthorDTO> GetNewBindingListAuthor()
        {
            return authorDAO.GetNewBindingListAuthor();
        }
        public bool UpdateAuthor(AuthorDTO author)
        {
            return authorDAO.UpdateAuthor(author);
        }
        public AuthorDTO GetAuthorByID(int id)
        {
            return authorDAO.GetAuthorByID(id);
        }

        public bool DeleteAuthor(int authorID)
        {
            if (authorDAO.CheckRelation(authorID))
            {
                return false;
            }

            return authorDAO.DeleteAuthor(authorID);
        }

        public void DeleteAuthor(List<int> authorID,out int success,out int fail)
        {
            success = 0;
            fail = 0;
            foreach (int id in authorID)
            {
                if (authorDAO.CheckRelation(id))
                {
                    fail++;
                }
                else
                {
                    if (authorDAO.DeleteAuthor(id))
                    {
                        success++;
                    }
                    else
                    {
                        fail++;
                    }
                }
            }
        }
    }
}
